package com.sk.bilibili.view.diy;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;

import com.bumptech.glide.Glide;
import com.sk.bilibili.bean.Them;
import com.sk.bilibili.bean.VideoSet;
import com.sk.bilibili.callBack.ThemClickCallBack;
import com.sk.bilibili.databinding.ViewThemUpdateItemBinding;
import com.sk.bilibili.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class UpdateGridView extends GridView {

    public UpdateGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
        updateGridAdapter=new UpdateGridAdapter(context);
        setAdapter(updateGridAdapter);
    }

    private UpdateGridAdapter updateGridAdapter;

    public UpdateGridAdapter getUpdateGridAdapter() {
        return updateGridAdapter;
    }

    static class UpdateGridAdapter extends BaseAdapter{

        private List<Them> list=new ArrayList<>();
        private Context context;
        private ThemClickCallBack themClickCallBack;

        public UpdateGridAdapter(Context context) {
            this.context = context;
        }

        public void setList(List<Them> list) {
            this.list = list;
            notifyDataSetChanged();
        }

        public void setThemClickCallBack(ThemClickCallBack themClickCallBack) {
            this.themClickCallBack = themClickCallBack;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewThemUpdateItemBinding bind;
            if(convertView==null){
                bind=ViewThemUpdateItemBinding.inflate(LayoutInflater.from(context),null,false);
                convertView=bind.getRoot();
                convertView.setTag(bind);
            }else {
                bind= (ViewThemUpdateItemBinding) convertView.getTag();
            }
            Them them = list.get(position);
            VideoSet videoSet = them.getExtra().getVideoSet();
            Integer updateNum = them.getExtra().getUpdateNum();
            Glide.with(context).load(them.getSmallCover()).into(bind.ivShow);
            bind.tvName.setText(videoSet.getTitle());
            bind.tvState.setText(StringUtils.getThemState(updateNum,them.getFinish()));
            bind.getRoot().setOnClickListener(v->themClickCallBack.onThemClick(them.getId()));
            return convertView;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

    }

}